home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tex / wsltex.p < prev    next >
Encoding:
Text File  |  1987-08-10  |  12.1 KB  |  538 lines

  1. PROGRAM Wsltex;
  2.     {
  3.     **********************************************************************
  4.     Copy an 8-bit file  to a 7-bit file,  turning characters with bit  8
  5.     set into 4-character octal escape sequences \nnn.  This is useful in
  6.     analyzing  WordStar  microcomputer   word  processor  files.    Many
  7.     sequences which  can be  recognized as  doing something  useful  are
  8.     turned into LaTeX command sequences.   Comments  below  describe the
  9.     sequences which are recognized and translated.
  10.  
  11.     Usage:
  12.  
  13.     @WSLTEX
  14.     Old 8-bit file: filespec
  15.     New 7-bit file: filespec
  16.  
  17.     [11-Oct-85]
  18.     **********************************************************************
  19.     }
  20. { ^L }
  21. CONST
  22.     nul = 0;
  23.  
  24.     bel = 7;
  25.  
  26.     ht = 9;
  27.     lf = 10;
  28.  
  29.     ff = 12;
  30.     cr = 13;
  31.  
  32.     esc = 27;
  33.  
  34.     maxring = 256; { limit for character ring }
  35.  
  36.     maxspecial = 100; { limit for warning when special strings (underline,
  37.               bold, etc) exceed this length }
  38.  
  39.     unset = MAXINT;
  40.  
  41. VAR
  42.     wordstar_file,latex_file : PACKED FILE OF integer;
  43.     ring_index : integer;
  44.     ring : PACKED ARRAY [0 .. maxring] OF integer;
  45.     hyphen_break : boolean;
  46.     eol,is_underline,is_subscript,is_superscript,is_bold,
  47.     is_alternate : boolean;
  48.     bi_eol,bi_underline,bi_subscript,bi_superscript,bi_bold,
  49.     bi_alternate : integer;
  50.     bo_eol,bo_underline,bo_subscript,bo_superscript,bo_bold,
  51.     bo_alternate : integer;
  52.     input_byte_offset,output_byte_offset : integer;
  53.     empty_count,last_c,c : integer;
  54. { ^L }
  55. PROCEDURE Warning(s : PACKED ARRAY [k1 .. k2 : integer] OF char;
  56.           b1,b2 : integer { output byte range, or unset,unset }
  57.           );
  58.     VAR       k : integer;
  59. BEGIN
  60.     Writeln(tty); { !!!! temporary for debugging !!!! }
  61.     Writeln(tty); { !!!! temporary for debugging !!!! }
  62.     Writeln(tty,'----------------------------------------------------------');
  63.     Write(tty,'%');
  64.  
  65.     FOR k := k1 TO k2 DO
  66.     Write(tty,s[k]);
  67.     Writeln(tty);
  68.     if b1 <> unset then
  69.             Writeln(tty,'    output byte range  = ',b1:0,' .. ',b2:0);
  70.     Writeln(tty,'    input byte offset  = ',input_byte_offset);
  71.     Writeln(tty,'    output byte offset = ',output_byte_offset);
  72.     Writeln(tty,'    current context = [');
  73.     FOR k := ring_index TO maxring DO
  74.     Write(tty,Chr(ring[k]));
  75.     FOR k := 0 TO ((maxring + ring_index - 1) MOD maxring) DO
  76.     Write(tty,Chr(ring[k]));
  77.     Writeln(tty);
  78.     Writeln(tty,'    ]');
  79.     Writeln(tty,'----------------------------------------------------------');
  80.     Writeln(tty)
  81. END; { Warning }
  82.  
  83. { ^L }
  84. PROCEDURE W_byte(c : integer);
  85. BEGIN
  86.     ring[ring_index] := c;
  87.     ring_index := (ring_index + 1) MOD maxring;
  88.     latex_file^ := c;
  89.     eol := (c = lf);
  90.     Put(latex_file);
  91.     output_byte_offset := output_byte_offset + 1;
  92.     IF eol THEN
  93.     BEGIN
  94.     bi_eol := input_byte_offset;
  95.     bo_eol := output_byte_offset
  96.     END
  97. END; { W_byte }
  98.  
  99. PROCEDURE W_char(c : char);
  100. BEGIN
  101.     W_byte(Ord(c))
  102. END; { W_char }
  103. { ^L }
  104. PROCEDURE W_octal;
  105. BEGIN
  106.     W_byte(Ord('\'));
  107.     W_byte((wordstar_file^ DIV 64) + Ord('0'));
  108.     W_byte(((wordstar_file^ DIV 8) MOD 8) + Ord('0'));
  109.     W_byte((wordstar_file^ MOD 8) + Ord('0'))
  110. END; { W_octal }
  111. { ^L }
  112. PROCEDURE W_string(s : PACKED ARRAY [k1 .. k2 : integer] OF char);
  113.     VAR k : integer;
  114. BEGIN
  115.     FOR k := k1 TO k2 DO
  116.     W_byte(Ord(s[k]))
  117. END; { W_string }
  118. { ^L }
  119. PROCEDURE Begin_alternate;
  120. BEGIN
  121.     IF is_alternate THEN
  122.     Warning(
  123. 'BEGIN ALTERNATE CHARACTER SET command encounted inside alternate sequence',
  124.         bo_alternate,output_byte_offset);
  125.     bi_alternate := input_byte_offset;
  126.     bo_alternate := output_byte_offset;
  127.     is_alternate := true;
  128.     W_string('{\alt ')
  129. END; { Begin_alternate }
  130. { ^L }
  131. PROCEDURE Begin_boldface;
  132. BEGIN
  133.     IF is_bold THEN
  134.     Warning('BEGIN BOLDFACE command encountered inside boldface',
  135.         bo_bold,output_byte_offset);
  136.     bi_bold := input_byte_offset;
  137.     bo_bold := output_byte_offset;
  138.     is_bold := true;
  139.     W_string('{\bf ')
  140. END; { Begin_boldface }
  141. { ^L }
  142. PROCEDURE End_alternate;
  143. BEGIN
  144.     IF is_alternate THEN
  145.     BEGIN
  146.         IF (bo_alternate + maxspecial) < output_byte_offset THEN
  147.             Warning('Long ALTERNATE CHARACTER SET sequence',
  148.             bo_alternate,output_byte_offset)
  149.     END
  150.     ELSE
  151.     Warning(
  152. 'END ALTERNATE CHARACTER SET command encountered outside alternate text',
  153.         bo_alternate,output_byte_offset);
  154.     is_alternate := false;
  155.     W_byte(Ord('}'))
  156. END; { End_alternate }
  157. { ^L }
  158. PROCEDURE End_boldface;
  159. BEGIN
  160.     IF is_bold THEN
  161.     BEGIN
  162.         IF (bo_bold + maxspecial) < output_byte_offset THEN
  163.             Warning('Long BOLDFACE sequence',bo_bold,output_byte_offset)
  164.     END
  165.     ELSE
  166.     Warning('END BOLDFACE command encountered outside boldface',
  167.         bo_bold,output_byte_offset);
  168.     is_bold := false;
  169.     W_byte(Ord('}'))
  170. END; { End_boldface }
  171. { ^L }
  172. PROCEDURE Line_feed;
  173. BEGIN { line_feed }
  174.     IF hyphen_break THEN { discard line break }
  175.         hyphen_break := false
  176.     ELSE
  177.     BEGIN
  178.         IF bo_eol < output_byte_offset THEN { text on line }
  179.          W_byte(lf)
  180.         ELSE { empty line }
  181.         BEGIN
  182.             IF last_c <> (128 + cr) THEN
  183.             W_byte(lf)
  184.         END
  185.        END
  186. END; { Line_feed }
  187. { ^L }
  188. PROCEDURE Superscript;
  189. BEGIN
  190.     IF is_superscript THEN { ending old superscript }
  191.     BEGIN
  192.          IF (bo_superscript + maxspecial) < output_byte_offset THEN
  193.          Warning('Long SUPERSCRIPT sequence',
  194.              bo_superscript,output_byte_offset);
  195.          W_byte(Ord('}'))
  196.     END
  197.     ELSE { beginning new superscript }
  198.     BEGIN
  199.         bi_superscript := input_byte_offset;
  200.         bo_superscript := output_byte_offset;
  201.         W_string('^{')
  202.     END;
  203.     is_superscript := NOT is_superscript
  204. END; { Superscipt }
  205. { ^L }
  206. PROCEDURE Subscript;
  207. BEGIN
  208.     IF is_subscript THEN { ending old superscript }
  209.     BEGIN
  210.          IF (bo_subscript + maxspecial) < output_byte_offset THEN
  211.          Warning('Long SUBSCRIPT sequence',
  212.             bo_subscript,output_byte_offset);
  213.          W_byte(Ord('}'))
  214.          END
  215.     ELSE { beginning new superscript  }
  216.     BEGIN
  217.         bi_subscript := input_byte_offset;
  218.         bo_subscript := output_byte_offset;
  219.         W_string('_{');
  220.     END;
  221.     is_subscript := NOT is_subscript
  222. END; { Subscript }
  223. { ^L }
  224. PROCEDURE Underline;
  225. BEGIN
  226.     IF is_underline THEN { ending old underline }
  227.     BEGIN
  228.         IF (bo_underline + maxspecial) < output_byte_offset THEN
  229.         Warning('Long UNDERLINE sequence',
  230.             bo_underline,output_byte_offset);
  231.         W_byte(Ord('}'))
  232.     END
  233.     ELSE { beginning new underline }
  234.     BEGIN
  235.         bi_underline := input_byte_offset;
  236.         bo_underline := output_byte_offset;
  237.         W_string('{\em ')
  238.     END;
  239.     { underlined text is \emphasized in LaTeX }
  240.     is_underline := NOT is_underline
  241. END; { Underline }
  242. { ^L }
  243. BEGIN { Main -- WSLTEX }
  244.  
  245. Rewrite(output,'tty:');
  246. Write('Input WordStar 8-bit file: ');
  247. Reset(wordstar_file,'':@,'/e/b:8');
  248. Write('Output LaTeX 7-bit file:   ');
  249. Rewrite(latex_file,'':@,'/e/b:7');
  250.  
  251. { Initializations -- in alphabetical order }
  252. bi_alternate := -1;
  253. bi_bold := -1;
  254. bi_subscript := -1;
  255. bi_superscript := -1;
  256. bi_underline := -1;
  257. bo_alternate := -1;
  258. bo_bold := -1;
  259. bo_subscript := -1;
  260. bo_superscript := -1;
  261. bo_underline := -1;
  262. eol := true;
  263. hyphen_break := false;
  264. input_byte_offset := -1;
  265. is_bold := false;
  266. is_subscript := false;
  267. is_superscript := false;
  268. is_underline := false;
  269. last_c := -1;
  270. output_byte_offset := -1;
  271.  
  272. for ring_index := 0 to maxring do
  273.     ring[ring_index] := ord(' ');
  274. ring_index := 0;
  275. {
  276. ========================================================================
  277. WordStar makes heavy use of the 8-th (high-order) bit of 8-bit ASCII
  278. characters.  In general, it turns on that bit for
  279. -- any character (0..127) at end-of-word
  280. -- any character (0..127) at end-of-line
  281. -- "hard" line break on CTL-M
  282. -- "hard" page break on CTL-L
  283.  
  284. A discretionary hyphen (which  can be removed on  joining the lines)  at
  285. end-of-line is encoded as "-\215" (i.e. "-<\200|CTL-M>").
  286.  
  287. A required hyphen falling at end-of-line is encoded as "-\040\215" (i.e.
  288. followed by a space).
  289.  
  290. Comment lines and WordStar command lines begin with a dot.
  291.  
  292. "Hard" line breaks are encoded as \215\015 (i.e. "<\200|CTL-M><CTL-J>");
  293. these occur between double-space lines.
  294.  
  295. "Soft" line breaks are encoded as <CTL-M><CTL-J>; these occur at
  296. paragraph ends.
  297.  
  298. Centered text is stored centered without any special markings.
  299.  
  300. Underlined text is bracketed by CTL-S.
  301.  
  302. Boldface text is bracketed by CTL-E ..text.. CTL-R.
  303.  
  304. Superscripts are bracketed by CTL-T.
  305.  
  306. Subscripts are bracketed by CTL-V.
  307.  
  308. Alternate typewheel strings appear as CTL-Q ..text.. CTL-W.  Some of
  309. these at least are mnemonic (e = epsilon, m = mu, . = centered dot).
  310.  
  311. CTL-R also appears after all sub/superscript sequences, apparently
  312. functioning as a "return to normal" state.
  313. ========================================================================
  314. }
  315.  
  316. c := wordstar_file^;
  317. WHILE NOT Eof(wordstar_file) DO
  318. BEGIN
  319.     IF c < 32 THEN { ordinary control character }
  320.     BEGIN
  321.     CASE c OF
  322.         0,1,2,3,4 :      { CTL-@ .. CTL-D }
  323.         W_octal;
  324.  
  325.         5 :      { CTL-E }
  326.         Begin_boldface;
  327.  
  328.         6,7,8 : { CTL-F .. CTL-H }
  329.         W_octal;
  330.  
  331.         9 :     { CTL-I }
  332.         W_byte(ht);
  333.  
  334.         10 :    { CTL-J }
  335.         Line_feed;
  336.  
  337.         11 :    { CTL-K }
  338.         W_octal;
  339.  
  340.         12 :    { CTL-L }
  341.         W_byte(ff);
  342.  
  343.         13 :    { CTL-M }
  344.             IF last_c = Ord('-') THEN
  345.             hyphen_break := true
  346.             ELSE IF NOT hyphen_break THEN
  347.                 W_byte(cr);
  348.  
  349.         14,15,16 :       { CTL-N .. CTL-P }
  350.         W_octal;
  351.  
  352.         17 :     { CTL-Q }
  353.         Begin_alternate;
  354.  
  355.         18 :     { CTL-R }
  356.             IF is_bold THEN
  357.             End_boldface;
  358.             { ELSE discard character }
  359.  
  360.         19 :     { CTL-S }
  361.         Underline;
  362.  
  363.         20 :     { CTL-T }
  364.         Superscript;
  365.  
  366.         21 :     { CTL-U }
  367.         W_octal;
  368.  
  369.         22 :     { CTL-V }
  370.         Subscript;
  371.  
  372.         23 :     { CTL-W }
  373.             End_alternate;
  374.  
  375.         24,25,26,27,28,29,30,31 :       { CTL-X .. CTL-_ }
  376.         W_octal
  377.         END { case }
  378.     END
  379.     ELSE IF chr(c) IN ['#','$','%','&','~','_','^','\','{','}']
  380.     THEN { LaTeX special characters must be quoted by backslash }
  381.     BEGIN
  382.         W_char('\');
  383.         W_byte(c)
  384.     END
  385.     ELSE IF c = Ord('.') THEN { check for WordStar comment }
  386.     BEGIN
  387.     IF eol THEN
  388.         W_string('%-WS-% ');
  389.     W_byte(c)
  390.     END
  391.     ELSE IF c < 128 THEN { c in 32 .. 127 }
  392.     W_byte(c)
  393.     ELSE { c > 127 }
  394.     BEGIN
  395.     c := c - 128; { lop off 8-th bit }
  396.     CASE c OF
  397.         0 : { CTL-@ }
  398.         W_octal;
  399.  
  400.         1 : { CTL-A }
  401.         W_octal;
  402.  
  403.         2 : { CTL-B }
  404.         W_octal;
  405.  
  406.         3 : { CTL-C }
  407.         W_octal;
  408.  
  409.         4 : { CTL-D }
  410.         W_octal;
  411.  
  412.         5 : { CTL-E }
  413.         Begin_boldface;
  414.  
  415.         6 : { CTL-F }
  416.         W_octal;
  417.  
  418.         7 : { CTL-G }
  419.         W_octal;
  420.  
  421.         8 : { CTL-H }
  422.         W_octal;
  423.  
  424.         9 : { CTL-I }
  425.         W_byte(ht);
  426.  
  427.  
  428.         10 :        { CTL-J }
  429.         Line_feed;
  430.  
  431.         11 :        { CTL-K }
  432.         W_octal;
  433.  
  434.         12 :        { CTL-L }
  435.         W_byte(ff);
  436.  
  437.  
  438.         13 :        { CTL-M }
  439.             IF last_c = Ord('-') THEN
  440.             hyphen_break := true
  441.             ELSE IF NOT hyphen_break THEN
  442.             BEGIN
  443.                 IF bo_eol < output_byte_offset THEN { not double space}
  444.                 W_byte(cr);
  445.             END;
  446.  
  447.         14 :        { CTL-N }
  448.         W_octal;
  449.  
  450.         15 :        { CTL-O }
  451.         W_octal;
  452.  
  453.         16 :        { CTL-P }
  454.         W_octal;
  455.  
  456.         17 :        { CTL-Q }
  457.         Begin_alternate;
  458.  
  459.         18 :        { CTL-R }
  460.             IF is_bold THEN
  461.             End_boldface;
  462.         { ELSE discard character }
  463.  
  464.         19 :        { CTL-S }
  465.         Underline;
  466.  
  467.         20 :        { CTL-T }
  468.         Superscript;
  469.  
  470.         21 :        { CTL-U }
  471.         W_octal;
  472.  
  473.         22 :        { CTL-V }
  474.         Subscript;
  475.  
  476.         23 :        { CTL-W }
  477.             End_alternate;
  478.  
  479.         24 :        { CTL-X }
  480.         W_octal;
  481.  
  482.         25 :        { CTL-Y }
  483.         W_octal;
  484.  
  485.         26 :        { CTL-Z }
  486.         W_octal;
  487.  
  488.         27 :        { CTL-[ }
  489.         W_octal;
  490.  
  491.         28 :        { CTL-\ }
  492.         W_octal;
  493.  
  494.         29 :        { CTL-] }
  495.         W_octal;
  496.  
  497.         30 :        { CTL-^ }
  498.         W_octal;
  499.  
  500.         31 :        { CTL-_ }
  501.         W_octal;
  502.  
  503.         32,33,34,35,36,37,38,39,40,41,42,43,44,45 :
  504.         W_byte(c);
  505.  
  506.         46 : { period }
  507.         BEGIN
  508.         IF eol THEN
  509.             W_string('%-WS-% ');
  510.         W_byte(c)
  511.         END;
  512.  
  513.         47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63 :
  514.         W_byte(c);
  515.  
  516.         64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
  517.         81,82,83,84,85,86,87,88,89,90,91,92,93,94,95 :
  518.         W_byte(c);
  519.  
  520.         96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,
  521.         111,112,113,114,115,116,117,118,119,120,121,122,123,124,
  522.         125,126 :
  523.         W_byte(c);
  524.  
  525.         127 :
  526.         W_octal;
  527.  
  528.         OTHERS :
  529.         W_octal;
  530.         END { CASE }
  531.     END;
  532.     last_c := wordstar_file^;
  533.     input_byte_offset := input_byte_offset + 1;
  534.     Get(wordstar_file);
  535.     c := wordstar_file^
  536. END;
  537. END. { Main -- WSLTEX }
  538.